package com.lx.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lx.book.ResultVo;
import com.lx.book.entity.Student;
import com.lx.book.service.StudentService;
import org.apache.catalina.manager.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;

    //分页带条件查询
    @GetMapping("/pageStudent")
    public ResultVo page(Student student, @RequestParam(defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        List<Student> list=studentService.getPage(student);
        PageInfo<Student> pageInfo=PageInfo.of(list);
        return ResultVo.success("",pageInfo);
    }

    //增加、修改
    @PostMapping
    public ResultVo addOrUpdate(@RequestBody Student student){
        if(student.getId()!=null){
            studentService.saveOrUpdate(student);
        }else {
            studentService.add(student);
        }

        return ResultVo.success("操作成功",null);
    }

    //删除
    @DeleteMapping("/{id}")
    public ResultVo del(@PathVariable Integer id){
        studentService.removeById(id);
        return ResultVo.success("操作成功",null);
    }

}
